const Alldata = {
	data() {
		return {
			isExist: true,
			eData: {},
			nData: {},
			wData: {},
			eBright: 0,
			nBright: 0,
			wBright: 0
		}
	},
	methods: {
		getData() {
			axios
				.get('http://172.22.112.171:8082/api')
				.then(response => {
					var allData = response.data;
					var item;
					// 将获取数据存入本地缓存
					var tpemData = localStorage.getItem(allData);
					localStorage.setItem("allDataMsg", JSON.stringify(allData));
					var str = window.localStorage ? localStorage.getItem("allDataMsg") : Cookie.read(
						"allDataMsg");
					// 将str转化成json
					str = JSON.parse(str);
					for (item in str) {
						var a = str[item];
						if ((a.device == 'RoadLight') && ((a.gateway == 'EGW_JDL_B') || (a.gateway ==
								'EGW_JSL') || (a.gateway == 'EGW_Dor8'))) {
							this.eData.set(a.device + '-' + a.addr, a);
						}
						if ((a.device == 'RoadLight') && ((a.gateway == 'NGW_511') || (a.gateway == 'NGW1'))) {
							this.nData.set(a.device + '-' + a.addr, a);
						}
						if ((a.device == 'RoadLight') & (a.gateway == 'WGW')) {
							this.wData.set(a.device + '-' + a.addr, a);
						}
					}
					
					// 将路灯各区个数存缓存
					var eCountMsg = localStorage.getItem(this.eData.size);
					localStorage.setItem("eCountMsg",this.eData.size);
					
					var wCountMsg = localStorage.getItem(this.wData.size);
					localStorage.setItem("wCountMsg",this.wData.size);
					
					var nCountMsg = localStorage.getItem(this.nData.size);
					localStorage.setItem("nCountMsg",this.nData.size);
					
					//统计全部路灯的数量
					this.countAllBright(this.eData, this.wData, this.nData)

					// 设置东区路灯超过一天没更新字体位置颜色变红色
					this.setClassNameToElement(this.eData, "outOfDate");

					// 设置西区路灯超过一天没更新字体位置颜色变红色
					this.setClassNameToElement(this.wData, "outOfDate");

					// 设置北区路灯超过一天没更新字体位置颜色变红色
					this.setClassNameToElement(this.nData, "outOfDate");
					
					// 设置东区路灯开灯状态颜色变黄
					this.setClassNameToOpenElement(this.eData, "openState");
					
					// 设置西区路灯开灯状态颜色变黄
					this.setClassNameToOpenElement(this.wData, "openState");
					
					// 设置北区路灯开灯状态颜色变黄
					this.setClassNameToOpenElement(this.nData, "openState");
					
				})
				.catch(function(error) { // 请求失败处理
					console.log(error);
				});
		},
		//获取时间戳
		time() {
			this.time = new Date().getTime();
		},
		get() {
			var eDataMsg = window.localStorage ? localStorage.getItem("eDataMsg") : Cookie.read(
				"eDataMsg");
			// 将str转化成json
			// eDataMsg = JSON.parse(eDataMsg);
			console.dir(eDataMsg)
		},
		setClassNameToElement(Data, className) {
			//判断参数Data是否为Map，不是则不做处理
			if (!(Data instanceof Map)){
				return;
			}
			Data.forEach((v, k) => {
				var delayedTime = v.time.slice(0, 10);
				var strTimeDate = new Date(Date.parse(delayedTime.replace('/-/g', '/')))
					.getTime();
				var timeStamp = this.time - strTimeDate;
				if (timeStamp >= 86400000) {
					var id = v.device + '-' + v.addr;
					var element = document.getElementById(id);
					if (element != null) {
						element.className = className;
					}
				}
			});
		},
		setClassNameToOpenElement(Data, className) {
			//判断参数Data是否为Map，不是则不做处理
			if (!(Data instanceof Map)){
				return;
			}
			Data.forEach((v, k) => {
				if (v.swtich == true) {
					var id = v.device + '--' + v.addr;
					var element = document.getElementById(id);
					if (element != null) {
						element.className = className;
					}
				}
			});
		},
		countBright (Data) {
			let count = 0;
			//判断参数Data是否为Map，不是则不做处理
			if (!(Data instanceof Map)){
				return;
			}
			Data.forEach((v, k) => {
				if (v.swtich == true) {
					count += 1;
				}
			})
			return count;
		},
		countAllBright (eData, wData, nData){
			// 计算东区亮灯个数
			this.eBright = this.countBright(eData);
			
			// 计算西区亮灯个数
			this.wBright = this.countBright(wData);
			
			// 计算北区亮灯个数
			this.nBright = this.countBright(nData);
		}
	},
	mounted() {
		var eData = new Map();
		var nData = new Map();
		var wData = new Map();
		this.eData = eData;
		this.nData = nData;
		this.wData = wData;
		this.getData();
		var that = this;
		setInterval(function() {
			that.getData();
		}, 1000);
		this.time();
		this.get();
	}
}